<?php

namespace App\Http\Service\OSSClient;

use App\Http\Service\BaseService;
use OSS\OssClient;
use OSS\Core\OssException;

class AliyunOSSClientService extends BaseService implements OSSClientInterface {

    private string $accessKeyId;
    private string $accessKeySecret;
    private string $endpoint;
    private string $bucket;
    private string $uploadCallbackUrl;
    private OssClient $ossClient;

    public function __construct() {
        $this->bucket = config("AliyunOSSClient.bucket_name");
        $this->uploadCallbackUrl = config("AliyunOSSClient.upload_callback_url");
        $config = [
            "accessKeyId" => config("AliyunOSSClient.access_key_id"),
            "accessSecret" => config("AliyunOSSClient.access_key_secret"),
            "endpoint" => config("AliyunOSSClient.endpoint")
        ];
        $this->setConfig($config);
        $this->connectOSS();
    }

    public function connectOSS(): mixed {
        try {
            $this->ossClient = new OssClient($this->accessKeyId, $this->accessKeySecret, $this->endpoint);
            return $this;
        }catch (OssException $e) {
            $this->setMessage(ERROR_CODE_OSS_CONNECT_EXCEPTION, $e->getMessage());
            return false;
        }
    }

    public function uploadFile($fileName, $filePath) {
        return false;
        $bucketExist = $this->checkBucketExist();
        if(empty($bucketExist)) {
            $this->setMessage(ERROR_CODE_OSS, "存储不存在");
            return false;
        }
        $callback = [
            "callbackUrl" => $this->uploadCallbackUrl,
            "callbackBody" => "bucket=" . $this->bucket . "&object=" . $fileName,
            "callbackBodyType" => "application/json"
        ];
        $options[OssClient::OSS_CALLBACK] = json($callback);
//        TODO::$options 回调
        $filePath = "/" . trim(base_path(), "/") . "/storage/" .  trim($filePath, "/");
        try {
            $this->ossClient->uploadFile($this->bucket, $fileName, $filePath);
            return true;
        }catch (OssException $e) {
            $this->setMessage(ERROR_CHECK_BUCKET_EXIST_EXCEPTION, $e->getMessage());
            return false;
        }
    }

    public function checkBucketExist($bucket = '') {
        if(empty($bucket)) {
            $bucket = $this->bucket;
        }
        try {
            $result = $this->ossClient->doesBucketExist($bucket);
            return $result;
        }catch (OssException $e) {
            $this->setMessage(ERROR_CHECK_BUCKET_EXIST_EXCEPTION, $e->getMessage());
            return false;
        }
    }

    public function setConfig($config): bool {
        $this->setAccessKeyId($config['accessKeyId']);
        $this->setAccessSecret($config['accessSecret']);
        $this->setEndpoint($config['endpoint']);
        return true;
    }

    public function setBucket($bucket) :bool{
        $this->bucket = $bucket;
        return true;
    }

    public function setAccessKeyId($accessKeyId) :bool {
        $this->accessKeyId = $accessKeyId;
        return true;
    }

    public function setAccessSecret($accessSecret) :bool {
        $this->accessKeySecret = $accessSecret;
        return true;
    }

    public function setEndpoint($endpoint) :bool {
        $this->endpoint = $endpoint;
        return true;
    }
}
